package client.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import packages.Packages;
import client.network.DrawMailBox;
import client.network.GUIMailBoxThread;
import client.network.MessageMailBox;

public class GUI extends JFrame implements Observer, ActionListener {
	private static int CANVAS_SIZE = 400;
	private static int USER_LIST_WIDTH = 100;
	private static int INFO_BAR_HEIGHT = 25;
	private int hightPadding = 10;
	private int widthPadding = 10;
	private Chat chat;
	private UserList userList;
	private JButton clear;
	private Canvas canvas;
	private InfoBar infoBar;
	private JButton start;
	private MessageMailBox messageMailBox;

	private boolean isDrawer;
	private boolean gameStarted;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public GUI(String name, MessageMailBox messageMailBox,
			DrawMailBox drawMailBox) {
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		this.messageMailBox = messageMailBox;

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("MultiPic - " + name);

		add(Box.createHorizontalStrut(widthPadding), c);

		c.gridx = 1;
		add(Box.createVerticalStrut(hightPadding), c);

		c.gridx = 4;
		add(Box.createHorizontalStrut(widthPadding), c);

		clear = new JButton();
		clear.setText("Clear canvas");
		clear.setEnabled(false);
		canvas = new Canvas(CANVAS_SIZE, CANVAS_SIZE, drawMailBox);
		infoBar = new InfoBar(CANVAS_SIZE, INFO_BAR_HEIGHT, canvas, clear,
				messageMailBox);
		c.gridx = 1;
		c.gridy = 1;
		c.gridheight = 2;
		add(new DrawWindow(infoBar, canvas), c);
		c.gridheight = 1;

		c.gridx = 2;
		add(Box.createHorizontalStrut(widthPadding), c);

		userList = new UserList(USER_LIST_WIDTH, CANVAS_SIZE + INFO_BAR_HEIGHT
				- 50);
		c.gridx = 3;
		add(userList, c);

		c.gridy = 2;
		start = new JButton("Start");
		start.setEnabled(false);
		start.addActionListener(this);
		add(start, c);

		c.gridy = 3;
		c.gridx = 1;
		c.gridwidth = 3;
		add(Box.createVerticalStrut(hightPadding), c);

		chat = new Chat(CANVAS_SIZE + USER_LIST_WIDTH + widthPadding, 50,
				messageMailBox);
		c.gridy = 4;
		add(chat, c);

		c.gridx = 1;
		c.gridy = 5;
		add(Box.createVerticalStrut(hightPadding), c);

		GUIMailBoxThread gui = new GUIMailBoxThread(messageMailBox);
		gui.addObserver(this);
		gui.addObserver(chat);
		gui.addObserver(userList);
		Thread messageThread = new Thread(gui);
		messageThread.start();

		pack();
		setVisible(true);
	}

	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof String) {
			String s = (String) arg;
			if (s.equals("DISCONNECT")) {
				JOptionPane.showMessageDialog(this,
						"The server disconnected.\nPress OK to quit.", "Error",
						JOptionPane.ERROR_MESSAGE);
				System.exit(-1);
			} else if (s.startsWith("Congratulation")) { // Någon har vunnit och
															// en ny runda ska
															// startas
				System.out.println("Game over");
				JOptionPane.showMessageDialog(this, arg);
				infoBar.setMessage("");
				isDrawer = false;
				canvas.disableCanvas();
				chat.emptyBox();
				start.setEnabled(false);
				clear.setEnabled(false);
			} else if (s.startsWith("#")) { // Ritaren får ett ord
											// att rita
				String temp = (String) arg;
				int reply = JOptionPane.showConfirmDialog(this,
						"The word is \"" + temp.substring(1) + "\". Accept?",
						"New word", JOptionPane.YES_NO_OPTION);
				if (reply == 0) {
					System.out.println("ANSWER: " + reply);
					infoBar.setMessage("Draw: " + temp.substring(1));
					clear.setEnabled(true);
					canvas.enableCanvas();
					isDrawer = true;
					messageMailBox.addOutgoingMessage("WORD_ACCEPTED");
				} else if (reply == 1) {
					messageMailBox.addOutgoingMessage("WORD_DENIED");
				}

			} else if (s.startsWith("!")) { // Ritaren vill tömma
											// canvas:en
				canvas.clear();
			} else if (s.startsWith("=")) { // Alla förutom ritaren
											// får antal
											// bokstäver till
											// ordet att gissa
				String temp = s.substring(1);
				infoBar.setMessage("The  word has " + temp + " letters");
			}
		} else if (arg instanceof String[]) { // Räknar antalet spelare
			System.out.println("Spelarlistan uppdateras");
			if (((String[]) arg).length >= 2 && !gameStarted) {
				start.setEnabled(true);
			}
		} else if (arg instanceof Byte) {
			if ((byte) arg == Packages.PACKAGE_NEW_GAME) {
				chat.enableChat();
				if (!isDrawer) {
					canvas.clear();
				}
				start.setEnabled(false);
				gameStarted = true;
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == start) {
			messageMailBox.addOutgoingMessage("START");
		}
	}

}
